package gatperdut.chato.core.handler.item;

import gatperdut.chato.core.ChatoServer;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.handler.ChatoHandler;
import gatperdut.chato.core.item.ItemController;
import gatperdut.chato.model.Chato.Item.Item;
import gatperdut.chato.model.Chato.Item.ItemFactory;
import gatperdut.chato.model.Chato.Item.ItemTemplate;
import gatperdut.chato.model.Chato.Util.UtilFactory;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.ecore.util.EcoreUtil;

public class ItemHandler extends ChatoHandler {

    private final Map<Item, ItemController> itemToController = new HashMap<Item, ItemController>();

    private final Map<String, ItemController> itemCDOIDToController = new HashMap<String, ItemController>();

    public ItemHandler( ChatoServer chatoServer ) {
        super( chatoServer );
    }

    public ItemController copyItemController( ItemController ic ) {
        Item newItem = EcoreUtil.copy( ic.getItem() );
        ItemController newIc = new ItemController( getChatoServer(), newItem );
        getItemToController().put( newItem, ic );
        return newIc;
    }

    public ItemController genItemController( ItemTemplate it ) {
        Item i = genItem( it );
        // --create the controller
        ItemController ic = new ItemController( getChatoServer(), i );
        // --tell server about the new item
        registerItem( i, ic );

        return ic;
    }

    private Item genItem( ItemTemplate it ) {

        Item i = ItemFactory.eINSTANCE.createItem();
        // --copy non-changeable stuff
        i.setShortDesc( it.getShortDesc() );
        i.setLongDesc( it.getLongDesc() );
        i.setFullDesc( it.getFullDesc() );
        i.getKeywords().addAll( it.getKeywords() );
        if ( it.getInventory() != null ) {
            i.setInventory( ItemFactory.eINSTANCE.createBoxInventory() );
            i.getInventory().setOpen( it.getInventory().isOpen() );
            if ( it.getInventory().getLock() != null ) {
                i.getInventory().setLock( UtilFactory.eINSTANCE.createLock() );
                i.getInventory().getLock().setLocked( it.getInventory().getLock().isLocked() );
                i.getInventory().getLock().getKeys().addAll( it.getInventory().getLock().getKeys() );
            }
            i.getInventory().setClosable( it.getInventory().isClosable() );
            Item newItem;
            for ( ItemTemplate itInside : it.getInventory().getItems() ) {
                newItem = genItem( itInside );
                i.getInventory().getItems().add( newItem );
            }
        }
        if ( it.getStack() != null ) {
            i.setStack( ItemFactory.eINSTANCE.createStack() );
            i.getStack().setCurrent( it.getStack().getCurrent() );
            i.getStack().setMax( it.getStack().getMax() );
        }
        if ( it.getFood() != null ) {
            i.setFood( ItemFactory.eINSTANCE.createFood() );
            i.getFood().setCurrent( it.getFood().getCurrent() );
            i.getFood().setMax( it.getFood().getMax() );
            i.getFood().setPortion( it.getFood().getPortion() );
            i.getFood().setMaxPortion( it.getFood().getMaxPortion() );
        }
        if ( it.getDrink() != null ) {
            i.setDrink( ItemFactory.eINSTANCE.createDrink() );
            i.getDrink().setFluidContent( it.getDrink().getFluidContent() );
            i.getDrink().setCurrent( it.getDrink().getCurrent() );
            i.getDrink().setCurrent( it.getDrink().getCurrent() );
        }
        if ( it.getWear() != null ) {
            i.setWear( ItemFactory.eINSTANCE.createWear() );
            i.getWear().getWearLocs().addAll( it.getWear().getWearLocs() );
        }
        // --set changeable stuff
        i.setW( it.getW() );
        // --set the template
        i.setTemplate( it );

        return i;
    }

    public void junkItem( ItemController ic ) {
        getChatoServer().getDatasource().getChato().getItemContainer().getItems().remove( ic.getItem() );
        getItemToController().remove( ic.getItem() );
    }

    public void registerItem( Item i, ItemController ic ) {
        getChatoServer().getDatasource().getChato().getItemContainer().getItems().add( i );
        getItemToController().put( i, ic );
    }

    public Map<Item, ItemController> getItemToController() {
        return itemToController;
    }

    public ItemController genCorpse( BaseController baseController ) {
        ItemTemplate corpseTemplate = getChatoServer().getItemTemplateHandler().getItemTemplateContainer().getCorpseTemplate();

        ItemController ic = genItemController( corpseTemplate );

        // inventory & descriptions
        ic.getItem().setShortDesc( "the corpse of " + baseController.getCharacter().getShortDesc() );
        ic.getItem().setLongDesc( "Here rests the corpse of " + baseController.getCharacter().getShortDesc() + "." );
        baseController.getInventoryController().dumpInto( ic.getInventoryController() );
        return ic;
    }

    public Map<String, ItemController> getItemCDOIDToController() {
        return itemCDOIDToController;
    }
}
